/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.dao.impl;

import cc.altius.dao.EmailDao;
import cc.altius.model.EmailTemplate;
import cc.altius.model.Emailer;
import cc.altius.utils.DateUtils;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author mudassar
 */
@Repository("EmailDao")
public class EmailDaoImpl implements EmailDao {

    private HibernateTemplate hibernateTemplate;
    private SessionFactory sessionFactory;
    private Session session;
    private Query query;

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }
    
    @Override
    @Transactional
    public List<EmailTemplate> getEmailTemplates() {
        return this.hibernateTemplate.find(" From EmailTemplate");
    }

    @Override
    @Transactional
    public int addEmail(int customerId, String emailId, String emailBody, int emailTemplateId,int status,String reason,String subject) {
        try {
            Date curDate = DateUtils.getCurrentDateObject(DateUtils.IST);
            session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
            //SmsDetails smsDetails = new SmsDetails();
            Emailer emailer = new Emailer();
            
            emailer.setCustomerId(customerId);
            emailer.setToSend(emailId);
            emailer.setSubject(subject);
            emailer.setBody(emailBody);
            emailer.setCreatedDate(curDate);
            emailer.setStatus(status);
            emailer.setReason(reason);
            EmailTemplate emailTemplate = new EmailTemplate();
            emailTemplate.setEmailTemplateId(emailTemplateId);
            emailer.setEmailTemplate(emailTemplate);
            
            int emailId1 = (Integer) this.hibernateTemplate.save(emailer);
            return emailId1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }
    
}
